# # api 接口初始化功能
# import json
#
# import redis
# from quart import (Quart, request, jsonify)
#
# from config import API
# from seasun.api.qtEvent import evq
# from seasun.model import initDB
# from seasun.session.redisSession import RedisSession
# from .auth import auth
# from .eventAPI import evi
# from .group import gp
# from .host import host
# from .product import product
#
#
# def register_api(app: Quart):
#     # pylint: disable=unused-variable
#     # create db engine
#     initDB(app)
#
#     # init session
#     RedisSession.init_session(app)
#
#     # register blueprints
#     app.register_blueprint(auth, url_prefix=API['url_prefix'] + '/auth')
#     app.register_blueprint(host, url_prefix=API['url_prefix'] + '/host')
#     app.register_blueprint(product, url_prefix=API['url_prefix'] + '/product')
#     app.register_blueprint(gp, url_prefix=API['url_prefix'] + '/group')
#     app.register_blueprint(evi, url_prefix=API['url_prefix'] + '/evi')
#     app.register_blueprint(evq, url_prefix=API['url_prefix'] + '/evq')
#
#     # middleware
#     @app.before_request
#     async def beforeRequest():
#         user = None
#         # get the token from http request's header
#         token = request.headers.get('qt-token', None)
#         # assign session handler to request object.
#         request.sessionHandler = redis.StrictRedis(connection_pool=app.sessionPool)
#         # do something
#         if token is not None:
#             user = request.sessionHandler.hgetall(token)
#             # session expired
#             if user:
#                 # check the user object is valid
#                 if 'roles' in user:
#                     user['roles'] = json.loads(user['roles'])
#                 if 'enterprise' in user:
#                     user['enterprise'] = json.loads(user['enterprise'])
#         # fetch the user's information from cache.
#         request.user = user
#
#     @app.after_request
#     async def afterRequest(response):
#         app.db.remove()
#         return response
#
#     @app.errorhandler(405)
#     async def error_handler_405(error):
#         message = f"this api don't allows Method: {request.method}, Please check the api document."
#
#         # 非请求json数据，直接返回提示信息
#         if request.content_type != 'application/json':
#             return message
#
#         return jsonify({
#             "success": False,
#             "message": message,
#             "data": {}
#         })
#
#     @app.errorhandler(404)
#     async def error_handler_404(error):
#         message = "the api is not exists, Please check the api document."
#
#         # 非请求json数据，直接返回提示信息
#         if request.content_type != 'application/json':
#             return message
#
#         return jsonify({
#             "success": False,
#             "message": message,
#             "data": {}
#         })
#
#     @app.errorhandler(500)
#     async def error_handler_500(error):
#         message = "Oops, something goes wrong, Please contact at the developers."
#
#         # 非请求json数据，直接返回提示信息
#         if request.content_type != 'application/json':
#             return message
#
#         return jsonify({
#             "success": False,
#             "message": message,
#             "data": {}
#         })
#
